package com.edu.medicalpro.kepu;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.edu.medicalpro.R;
import com.edu.medicalpro.activity.MainActivity;
import com.edu.medicalpro.base.BaseFragment;
import com.edu.medicalpro.kepuyemian.KepuyemianFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class KePuFragment extends BaseFragment {
    private List<String> mtable= Arrays.asList("关注", "推荐", "热榜", "发现", "西安");
    private  List<KepuyemianFragment>  NewsListBlankFragments=new ArrayList<>();
    private   List<String> mtable1= Arrays.asList("top", "tuijian", "rebang", "faxian","xian");
    @Override
    protected void initView(View rootView) {
        TabLayout news = rootView.findViewById(R.id.news);
        ViewPager news1=rootView.findViewById(R.id.news1);
        for (int i=0;i<mtable.size();i++){
            NewsListBlankFragments.add(new KepuyemianFragment());
        }
        NewsPagerAdater  adapter=new NewsPagerAdater (
                requireFragmentManager(),FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        news1.setAdapter(adapter);
        news.setupWithViewPager(news1);
        news1.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                ((MainActivity)requireActivity()).getViewModel().setNewstype(mtable1.get(position));
            }
        });
}
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_kepu;
    }

    @Override
    protected void initData() {

    }
    class NewsPagerAdater extends FragmentPagerAdapter {
        public NewsPagerAdater(FragmentManager fm, int behavior) {
            super(fm, behavior);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return NewsListBlankFragments.get(position);
        }
        @Override
        public int getCount() {
            return NewsListBlankFragments.size();
        }
        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return mtable.get(position);
        }
    }
}
